package com.hzdracom.zhangnn.gradientviewdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Parcelable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 项目名称 GradientViewDemo.
 * 包名 com.hzdracom.zhangnn.gradientviewdemo
 * 创建者 zhangnn
 * 创建时间  2016/8/17 13:48.
 * 备注 ：颜色渐变控件（类似微信朋友圈底部Tab）
 */
public class GradientIconView extends LinearLayout {
    private Context mContext;
    private ImageView mFrontIcon;
    private ImageView mBottomIcon;
    private TextView mFrontText;
    private TextView mBottomText;
    private RelativeLayout mIconView;
    private RelativeLayout mTextView;
    private float mAlpha = 0;
    private float textSizeDefault = 12;
    private static final String INSTANCE_STATE = "instance_state";
    private static final String STATE_ALPHA = "state_alpha";

    public GradientIconView(Context context) {
        this(context, null);

    }

    public GradientIconView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public GradientIconView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        initView();

        // 获取设置的属性
        TypedArray a = context.obtainStyledAttributes(attrs,
                R.styleable.GradientIconView);

        BitmapDrawable drawable;

        int n = a.getIndexCount();
        for (int i = 0; i < n; i++) {

            int attr = a.getIndex(i);
            switch (attr) {
                case R.styleable.GradientIconView_unselected_icon:
                    drawable = (BitmapDrawable) a.getDrawable(attr);
                    setBackgroundIconView(drawable);
                    break;
                case R.styleable.GradientIconView_selected_icon:
                    drawable = (BitmapDrawable) a.getDrawable(attr);
                    setFrontIconView(drawable);
                    break;
                case R.styleable.GradientIconView_text_content:
                    String textContent = a.getString(attr);
                    setTextContent(textContent);
                    break;
                case R.styleable.GradientIconView_text_size:
//                    float textSize =  a.getDimension(attr, TypedValue
//                            .applyDimension(TypedValue.COMPLEX_UNIT_SP, 10,
//                                    getResources().getDisplayMetrics()));
                    //此处是为了转化字体大小，getdimension()返回的是一个转化后的PX值
                    float textSize = px2Sp(mContext, a.getDimension(attr, textSizeDefault));
                    setTextSize(textSize);
                    break;
                case R.styleable.GradientIconView_unselected_text_color:
                    int textColorUnselected = a.getColor(attr, Color.BLACK);
//                    textColor = a.getColor(attr, textColorDefault);
                    setTextColorUnselected(textColorUnselected);
                    break;

                case R.styleable.GradientIconView_selected_text_color:
                    int textColorSelected = a.getColor(attr, Color.GRAY);
//                    textColor = a.getColor(attr, textColorDefault);
                    setTextColorSelected(textColorSelected);
                    break;
                case R.styleable.GradientIconView_icon_visibility:
                    int iconVisibility = a.getInt(attr, 0);
                    setIconVisibility(iconVisibility);
                    break;
                case R.styleable.GradientIconView_text_visibility:
                    int textVisibility = a.getInt(attr, 0);
                    setTextVisibility(textVisibility);
                    break;
                case R.styleable.GradientIconView_icon_margin_top:
                    float marginTop =  px2Dp(mContext, a.getDimension(attr, 4));
                    setIconMarginTop(marginTop);
                    break;
                case R.styleable.GradientIconView_text_margin_bottom:
                    float marginBottom =  px2Dp(mContext, a.getDimension(attr, 4));
                    setTextMarginBottom(marginBottom);
                    break;

                default:
                    break;

            }
        }
        a.recycle();
        setViewAlpha(mAlpha);
    }

    /**
     * 设置图标是否显示
     * @param visibility
     */
    public void setIconVisibility(int visibility) {
        mIconView.setVisibility(visibility);
    }


    /**
     * 设置文本是否显示
     * @param visibility
     */
    public void setTextVisibility(int visibility) {
        mTextView.setVisibility(visibility);
    }

    /**
     * 设置图标到顶部的边距(相对于底部tab)
     * @param marginTop 单位DP
     */
    public void setIconMarginTop(float marginTop) {
        int top = (int) dp2Px(mContext,marginTop);
        mIconView.setPadding(0, top, 0, 0);
    }

    /**
     * 设置文本到底部的边距(相对于底部tab)
     * @param marginBottom 单位 DP
     */
    public void setTextMarginBottom(float marginBottom) {
        int bottom = (int) dp2Px(mContext,marginBottom);
        mTextView.setPadding(0, 0, 0, bottom);
    }

    /**
     * 设置未选中时的图片
     * @param drawable
     */
    public void setBackgroundIconView(BitmapDrawable drawable) {
        mBottomIcon.setBackground(drawable);
    }

    /**
     * 设置选中时的图片
     * @param drawable
     */
    public void setFrontIconView(BitmapDrawable drawable) {
        mFrontIcon.setBackground(drawable);
    }

    /**
     * 设置文本内容
     *
     * @param textContent
     */
    public void setTextContent(String textContent) {
        mFrontText.setText(textContent);
        mBottomText.setText(textContent);
        mBottomText.setVisibility(VISIBLE);
    }

    /**
     * 设置文本未选中时的颜色
     *
     * @param textColor
     */
    public void setTextColorUnselected(int textColor) {
        mBottomText.setTextColor(textColor);
    }

    /**
     * 设置文本选中时的颜色
     *
     * @param textColor
     */
    public void setTextColorSelected(int textColor) {
        mFrontText.setTextColor(textColor);
    }

    /**
     * 设置文本字体大小
     *
     * @param textSize 单位SP
     */
    public void setTextSize(float textSize) {
        mFrontText.setTextSize(textSize);
        mBottomText.setTextSize(textSize);
    }

    /**
     * 实现根据滑动的距离来改变控件的颜色值变化
     *
     * @param mAlpha float 0-1
     */
    public void setViewAlpha(float mAlpha) {
        if (mAlpha > 1 || mAlpha < 0) {
            throw new IllegalArgumentException("mAlpha value is illegal");
        }
        mFrontIcon.setAlpha(mAlpha);
        mBottomIcon.setAlpha(1 - mAlpha);
        mFrontText.setAlpha(mAlpha);
        mBottomText.setAlpha(1 - mAlpha);
        this.mAlpha = mAlpha;
    }


    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.gradient_icon_tab_view, this, true);
        mFrontIcon = (ImageView) findViewById(R.id.gradient_front_icon);
        mBottomIcon = (ImageView) findViewById(R.id.gradient_background_icon);
        mFrontText = (TextView) findViewById(R.id.gradient_front_text);
        mBottomText = (TextView) findViewById(R.id.gradient_background_text);
        mIconView = (RelativeLayout) findViewById(R.id.gradient_icon_rlay);
        mTextView = (RelativeLayout) findViewById(R.id.gradient_text_rlay);
    }

    @Override
    protected Parcelable onSaveInstanceState() {
        Bundle bundle = new Bundle();
        bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());
        bundle.putFloat(STATE_ALPHA, mAlpha);
        return bundle;
    }

    @Override
    protected void onRestoreInstanceState(Parcelable state) {
        if (state instanceof Bundle) {
            Bundle bundle = (Bundle) state;
            mAlpha = bundle.getFloat(STATE_ALPHA);
            super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));
        } else {
            super.onRestoreInstanceState(state);
        }

    }


    private float px2Sp(Context context, float pxVal) {
        return (pxVal / context.getResources().getDisplayMetrics().scaledDensity);
    }

    private float px2Dp(Context context, float pxVal) {
        return (pxVal / context.getResources().getDisplayMetrics().density);
    }

    private float dp2Px(Context context, float dpVal) {
        return  (dpVal * context.getResources().getDisplayMetrics().density);
    }
}
